package nio;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class Demo5 {

    public static void main(String[] args) throws CharacterCodingException {
        //获取编码器
        Charset charset = Charset.forName("GBK");
        //获取加密器
        CharsetEncoder ce = charset.newEncoder();
        //获取解码器
        CharsetDecoder cd = charset.newDecoder();
        CharBuffer charBuffer = CharBuffer.allocate(1024);
        charBuffer.put("李博飞");
        charBuffer.flip();
        //编码加密
        ByteBuffer byteBuffer = ce.encode(charBuffer);
        for(int i = 0 ; i < 6 ; i++){
            System.out.println((char) byteBuffer.get());
        }

        byteBuffer.flip();
        Charset c2 = Charset.forName("GBK");
        //编码解密
        CharBuffer decode2 = c2.newDecoder().decode(byteBuffer);
        System.out.println(decode2.toString());

    }
}
